﻿using System;
using System.Collections;
using Tipler.Siniflar;
using System.Configuration;
using DataServis;
using System.Reflection;

public class TemelIsKurali : MarshalByRefObject
{

    protected string KullaniciID;
    protected string IlID;
    private VeriArabirim VeriKatmani;
    public TemelIsKurali IsKuraliOlustur(Type oType)
    {
        TemelIsKurali o = (TemelIsKurali)Assembly.GetAssembly(oType).CreateInstance(oType.FullName);
        if (VeriKatmani != null)
        {
            if (VeriKatmani.OzellikTransactionalMi)
            {
               o.VeriKatmani= VeriKatmani;
            }
        }
        return o;
    }

    public VeriArabirim VeriTabaniObjesiGetir(bool Transactional)
    {
        if (Transactional)
        {
            if (VeriKatmani != null)
            {
                if (VeriKatmani.OzellikTransactionalMi)
                {
                    return VeriKatmani;
                }
                else
                {
                    VeriKatmani =  VeriServis.VeriTabaniObjesiGetir(true);
                }

            }
            else
            {
                VeriKatmani = VeriServis.VeriTabaniObjesiGetir(true);
            }

        }
        else
        {
            VeriKatmani = VeriServis.VeriTabaniObjesiGetir(false);
        }
        return VeriKatmani;
    }

    public TemelIsKurali()
    {
        if ((System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("KullaniciID")) != null))
        {
            KullaniciID = System.Threading.Thread.GetData(System.Threading.Thread.GetNamedDataSlot("KullaniciID")).ToString();
        }
        else
        {
            KullaniciID = "";
        }
    }


    protected string SorguWhereCumlesiHazirla(Hashtable htSorguKriterleri)
    {
        return SorguWhereCumlesiHazirla(htSorguKriterleri, null);
    }
    protected string SorguWhereCumlesiHazirla(Hashtable htSorguKriterleri, Hashtable htSorguOperatorleri)
    {
        string sWhere = "";
        try
        {
            foreach (string sKey in htSorguKriterleri.Keys)
            {
                if ((htSorguOperatorleri == null))
                {
                    sWhere += WhereCumlesiHazirla(sKey, htSorguKriterleri[sKey]) + " AND ";
                }
                else
                {
                    sWhere += WhereCumlesiHazirla(sKey, htSorguKriterleri[sKey], htSorguOperatorleri[sKey].ToString()) + " AND ";
                }
            }
            if (!string.IsNullOrEmpty(sWhere))
            {
                sWhere = " WHERE " + sWhere.Substring(0, sWhere.Length - 5);
            }
        }
        catch
        {
            sWhere = "";
        }
        return sWhere;
    }
    private string WhereCumlesiHazirla(string sKey, object oDeger)
    {
        return WhereCumlesiHazirla(sKey, oDeger, "=");
    }
    private string WhereCumlesiHazirla(string sKey, object oDeger, string sOperator)
    {
        if (oDeger.GetType().Name == typeof(string).Name ||
            oDeger.GetType().Name == typeof(Guid).Name)
        {
            return sKey + " " + sOperator + " '" + oDeger.ToString() + "'";
        }
        else if (oDeger.GetType().Name == typeof(int).Name ||
            oDeger.GetType().Name == typeof(long).Name ||
            oDeger.GetType().Name == typeof(int).Name)
        {
            return sKey + "=" + oDeger.ToString();
        }
        else if (oDeger.GetType().Name == typeof(ArrayList).Name)
        {
            if (((ArrayList)oDeger)[0] is int)
            {
                string sRet = sKey + " IN (";
                foreach (int nDeger in (ArrayList)oDeger)
                {
                    sRet += nDeger.ToString() + ",";
                }
                if (sRet != sKey + " IN (")
                {
                    sRet = sRet.Substring(0, sRet.Length - 1) + ")";
                }
                else
                {
                    sRet = "";
                }
                return sRet;
            }
            else if (((ArrayList)oDeger)[0] is Guid)
            {
                string sRet = sKey + " IN ('";
                foreach (Guid nDeger in ((ArrayList)oDeger))
                {
                    sRet += nDeger.ToString() + "','";
                }
                if (sRet != sKey + " IN ('")
                {
                    sRet = sRet.Substring(0, sRet.Length - 2) + ")";
                }
                else
                {
                    sRet = "";
                }
                return sRet;
            }
            else
                return "=";
        }
        else
            return sKey + "=" + oDeger.ToString() + "";
    }


    public string Concat
    {
        get
        {
            if (ConfigurationManager.AppSettings["DBProvider"].ToString() == "MSSQL")
                return " + ";
            else  //Oracle
                return " || ";
        }
    }

    public string PrmSgn
    {
        get
        {
            if (ConfigurationManager.AppSettings["DBProvider"].ToString() == "MSSQL")
                return "@";
            else  //Oracle
                return ":";
        }
    }


    public string FncUsr
    {
        get
        {
            if (ConfigurationManager.AppSettings["DBProvider"].ToString() == "MSSQL")
                return "dbo.";
            else  //Oracle
                return "";
        }
    }
}
